package com.example.gitpart03;
import android.content.Context; // 添加 Context 导入
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;

public class MyselfFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载 fragment_myself.xml 布局
        View view = inflater.inflate(R.layout.fragment_myself, container, false);
        // 找到亲爱的奶粉和注册/登录的 TextView
        TextView dearNaifenText = view.findViewById(R.id.dear_naifen_text);
        TextView registerLoginText = view.findViewById(R.id.register_login_text);

        // 获取登录成功标志位
        Bundle args = getArguments();
        if (args != null && args.getBoolean("isLoggedIn", false)) {
            registerLoginText.setText("心中有Lu");
        }

        // 设置点击事件
        dearNaifenText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Login.class);
                startActivity(intent);
            }
        });
        registerLoginText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Login.class);
                startActivity(intent);
            }
        });
        // 获取 ListView
        ListView listMine = view.findViewById(R.id.list_mine);
        // 创建数据列表
        List<ListItem> dataList = new ArrayList<>();
        dataList.add(new ListItem(R.drawable.yogurt_heart, "会员权益 1"));
        dataList.add(new ListItem(R.drawable.yogurt_heart, "会员权益 2"));
        dataList.add(new ListItem(R.drawable.yogurt_heart, "会员权益 3"));
        dataList.add(new ListItem(R.drawable.yogurt_heart, "会员权益 4"));
        dataList.add(new ListItem(R.drawable.yogurt_heart, "会员权益 5"));
        // 创建自定义适配器
        ListItemAdapter adapter = new ListItemAdapter(
                getContext(),
                R.layout.mine_list_item,
                dataList
        );
        // 设置适配器
        listMine.setAdapter(adapter);
        return view;
    }
    // 自定义适配器类
    private static class ListItemAdapter extends ArrayAdapter<ListItem> {
        private final int resourceId;
        // 确保 Context 被正确使用
        public ListItemAdapter(Context context, int resource, List<ListItem> objects) {
            super(context, resource, objects);
            this.resourceId = resource;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder holder;
            if (convertView == null) {
                // 加载布局
                LayoutInflater inflater = LayoutInflater.from(getContext());
                view = inflater.inflate(resourceId, parent, false);
                // 创建 ViewHolder
                holder = new ViewHolder();
                holder.imageView = view.findViewById(R.id.img_list_mine);
                holder.textView = view.findViewById(R.id.text_list_mine);
                // 将 ViewHolder 存储在视图中
                view.setTag(holder);
            } else {
                // 重用已有的视图
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }
            // 获取当前项的数据
            ListItem item = getItem(position);
            if (item != null) {
                // 设置图标和文本
                holder.imageView.setImageResource(item.getImageResId());
                holder.textView.setText(item.getText());
            }
            return view;
        }
        // ViewHolder 类用于优化性能
        private static class ViewHolder {
            ImageView imageView;
            TextView textView;
        }
    }
}